﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FukiERP.Common.Constants;
using Telerik.WinControls.UI;
using FukiERP.Services.DBContext;

namespace FukiERP.Helper {
    public class PresentHelper {
        public static bool ShowConfirmBox(string message){
            return MessageBox.Show(message, MessageConstants.ConfirmTitle, 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
        }

        public static void ClearTextBox(params Control[] controls) {
            foreach (Control c in controls) {
                c.Text = string.Empty;
            }
        }
        public static void ClearDropBox(params RadDropDownList[] controls) {
            foreach (RadDropDownList c in controls) {
                c.SelectedIndex = -1;
                c.Text = CommonConstants.DefaultDropDownValue;
            }
        }


        public static void LoadProductCate(FukiERPDataContext context, RadDropDownList control) {
            List<ProductCategory> cates = context.ProductCategories.OrderBy(c => c.Parent).ToList();
            List<RadListDataItem> drpCateItem = new List<RadListDataItem>();
            foreach (ProductCategory cate in cates) {
                if (cate.Parent != 0) {
                    break;
                }
                drpCateItem.Add(new RadListDataItem(cate.Name, cate.ID));
                BuildChildItems(cate.ID, cates, drpCateItem, string.Empty);
            }
            control.DataSource = drpCateItem;
            control.Text = CommonConstants.DefaultDropDownValue;
        }

        private static void BuildChildItems(int cateid, List<ProductCategory> cates, List<RadListDataItem> items, string levelMark) {
            List<ProductCategory> subCates = cates.Where(c => c.Parent == cateid).ToList();
            if (subCates.Count() == 0) {
                return;
            } else {
                levelMark = levelMark + CommonConstants.SubCatePreFix;
                foreach (ProductCategory cate in subCates) {
                    items.Add(new RadListDataItem(levelMark + cate.Name, cate.ID));
                    BuildChildItems(cate.ID, cates, items, levelMark);
                }
            }
        }

        public static void AddFirstItemToDropDownList(RadDropDownList drp, string text) {
            drp.Items.Insert(0, new RadListDataItem(text));
        }
    }
}
